<template>
    <view id="reviewEditon" v-show="isLoading">
        <view class="form">
        	<view class="form-title">姓名</view>
        	<input class="uni-input" name="name" maxlength="20" v-model="name" placeholder="请输入姓名" />
            <view class="form-action"></view>
        </view>
        <view class="form">
        	<view class="form-title">证件号</view>
        	<input class="uni-input" name="idcard" type="idcard" maxlength="30" v-model="idcard" placeholder="请输入证件号" />
            <view class="form-action"></view>
        </view>
        <view class="form top-margin">
        	<view class="form-title">级别</view>
            <picker class="picker"  :value="indexOfRank" @change="rankChange" range-key="label" :range="rankList">
                <input class="uni-input" name="rank" disabled="true" :value="rank" placeholder="请选择级别" />
                <uni-icons class="picker-icons" type="arrowright"  size="20" />
            </picker>
        </view>
        <view class="form">
        	<view class="form-title">成绩</view>
            <input class="uni-input" name="grade" v-model="grade" type="number"  placeholder="请填写成绩" />
            <!-- <uni-icons class="picker-icons" type="arrowright"  size="20" /> -->
        </view>
        <button @click="submitFun" class="btn" type="primary" >确定修改</button>
    </view>
</template>
<script>
    import uniIcons from "@/components/uni-icons/uni-icons.vue"
    import validate from '@/common/ys-validate.js'
    import { rankExtent, updateCert } from "@/common/api.js"
    export default {
        name: "reviewEditon",
        data() {
            return {
                name:'',
                idcard:'',
                indexOfRank:0,
                rankList:[],
                rank:'',
                grade:'',
                rankValue:'',
                logs_id:'',
                rank_type:'',
                isLoading:false,
            };
        },
        components: {
            uniIcons
        },
        methods: {
            rankChange(e){
                this.rank=this.rankList[e.detail.value].label;
                this.rankValue=this.rankList[e.detail.value].value
            },
            submitFun(){
                if(!/^[a-zA-Z\u4e00-\u9fa5]+$/.test(this.name)){
                    uni.showToast({
                        icon: 'none',
                        title: '请输入正确姓名'
                    })
                    return;
                }
                if(!/^\w+$/.test(this.idcard)){
                    uni.showToast({
                        icon: 'none',
                        title: '请输入正确证件号'
                    })
                    return;
                }
                if(!this.rank){
                    uni.showToast({
                        icon: 'none',
                        title: '请选择级别'
                    })
                    return;
                }
                if(!this.grade){
                    uni.showToast({
                        icon: 'none',
                        title: '请输入成绩'
                    })
                    return;
                }
                updateCert({
                    logs_id:this.logs_id,
                    levelID:this.rankValue,
                    ranking:this.rank_type!=1?this.grade*100:this.grade,
                    idCardName:this.name,
                    idcard:this.idcard
                }).then((res)=>{
                    const data = res.data;
                    if(data.errcode==0){
                        uni.showModal({
                            title: '提示',
                            content: '修改成功',
                            success: function (res) {
                                if (res.confirm) {
                                    uni.navigateBack({
                                        delta: 1
                                    });
                                }
                            }
                        });
                    }else if(data.errcode==1000 || data.errcode==1001){
                        uni.removeStorageSync('token');
                        uni.reLaunch({
                        	url:'/pages/home/welcome'
                        })
                    }else{
                        uni.showToast({
                        	icon: 'none',
                        	title:data.msg
                        });
                    }
                })
            },
            orgLevelList(){
                let p = rankExtent({type:'all'}).then((res)=>{
                    const data = res.data;
                    if(data.errcode==0){
                        this.rankList=[];
                        data.data.forEach((e)=>{
                            this.rankList.push({
                                value:e.eventType_id >> 0,
                                label:e.type_name,
                                event_value:e.event_value >> 0
                            })
                        })
                    }else if(data.errcode==1000 || data.errcode==1001){
                        uni.removeStorageSync('token');
                        uni.reLaunch({
                        	url:'/pages/home/welcome'
                        })
                    }else{
                        uni.showToast({
                        	icon: 'none',
                        	title:data.msg
                        });
                    }
                })
                return p;
            },
            getRankName(value){
                let name = this.rankList.find(e=>e.value==value);
                return name.label
            }
        },
        onShow() {
            uni.showLoading({
                title: '加载中'
            });

            let data = uni.getStorageSync('_memberEditon_')
            this.logs_id = data.logs_id;
            this.grade = data.ranking;
            this.name = data.name;
            this.idcard = data.idcard;
            this.rank_type = data.rank_type;
            this.rankValue = data.level;
            let init = async ()=>{
                await this.orgLevelList();
                this.rank = this.getRankName(data.level)
                await uni.hideLoading()
                this.isLoading=true;
            }
            init()
        }
    };
</script>
<style lang="scss">
    #reviewEditon {
        position: relative;
        height: 100%;
        padding: 15px;
        .top-margin{
            margin-top: 20rpx;
        }
        .form{
            display: flex;
            align-items: center;
            flex: 1;
            background: #fff;

            flex-direction: row;
            justify-content: space-between;
            .picker{
                width: 100%;
                margin-left: 40rpx;
                position: relative;
                .picker-icons{
                    bottom: 0px;
                    right: 40rpx;
                    position: absolute;
                }
            }
            .form-title{
                padding: 20rpx;
                padding-left: 30rpx;
                width: 150rpx;
            }
            .form-action{
                padding: 10rpx 30rpx;
                display: flex;
                .sendBtn{
                    color: #000;
                }
            }
        }
        .btn{
            margin: 30rpx;
            font-size: 32rpx;
            margin-top: 60rpx;
            border-radius: 50rpx;
        }
    }
</style>
